<?php
class CustLocationsController extends AppController {
    var $name = 'CustLocations';

    var $uses = array('CustLocation', 'Site', 'CustProductSite', 'Product', 'CPSLocation');

    var $viewPath = 'customers/inventories/cust_locations';

    var $pageTitle = 'Site Location';

    var $paginate = array('CustLocation' => array('order' => array('CustLocation.name')));

    function beforeFilter() {
        parent::beforeFilter();
        $this->set('parentNav', 'cust_product_sites');
        $this->set('tabMenu', 'tabmenus/customer/inventories');
        $this->viewSiteInventoryId = $this->Session->check('Auth.User.Extend.view_site_inventory')?
        $this->Session->read('Auth.User.Extend.view_site_inventory'): $this->Auth->user('site_id');
    }

    function display() {
        $this->data['CustLocation']['site_id'] = $this->Auth->user('site_id');
        parent::display('CustLocation');
        $this->_paging(Inflector::classify($this->name), 'LIKE');
        $this->set('site', $this->Site->findById($this->Auth->user('site_id')));
    }

    function save() {
        $cust_location_id = null;
        if (!empty($this->data)) {
            $this->data['CustLocation']['site_id'] = $this->Auth->user('site_id');
            $this->data['CustLocation']['collector_id'] = $this->Auth->user('id');
            if($this->CustLocation->save($this->data)) {
                $this->Session->setFlash("Location {$this->data['CustLocation']['name']} save succesfull.", 'flash/ok');
                $cust_location_id = $this->CustLocation->id;
            } else {
                $this->Session->setFlash("Location save fail.", 'flash/fail');
                // ถ้าเป็น mode edit เก็บ id เอาไว้กรณีบันทึกข้อมูลผิด
                $cust_location_id = $this->data['CustLocation']['id'];
            }
        }
        $this->setAction('view', $cust_location_id);
    }

    /**
     * @todo แสดงตาราง Product ทั้งหมดที่อยู่ใน Location นี้
     * @param <type> location id
     */
    function view($id = null) {
        if ($id != null) {
            $this->data = $this->CustLocation->findById($id);
            $this->CustLocation->id = $id;
            $this->set('neighbors', $this->CustLocation->find('neighbors', array('fields' => array('id', 'name'))));
            $this->set('mode', 'edit');
        } else {
            $this->set('mode', 'add');
        }
        $this->Site->id = $this->Auth->user('site_id');
        $this->pageTitle = $this->Site->field('name') . ' Site Location';
    }

    function popup_search() {
        parent::popup_search();
    }

    /**
     * list รายการ CustLocation ที่ CustProductSite สามารถเลือก Stock in ได้
     * ถ้าไม่มีใน CPSLocation ดึงรายการ location ทั้งหมดของ Site นั้นแทน
     * @param <type> $cust_product_site_id
     */
    function option_cps_location_list($cust_product_site_id) {
        // Subquery ดึงรายการ Location ที่อยู่ใน CPSLocation ของ CustProductSite นี้
        $conditionsSubQuery['CPSLocation.cust_product_site_id'] = $cust_product_site_id;
        $dbo = $this->CPSLocation->getDataSource();
        $subQuery = $dbo->buildStatement(
                array(
                'fields' => array('CPSLocation.cust_location_id'),
                'table' => $dbo->fullTableName($this->CPSLocation),
                'alias' => 'CPSLocation',
                'conditions' => $conditionsSubQuery,
                'order' => null, 'limit' => null, 'group' => null,
                ),
                $this->CPSLocation
        );
        $subQuery = ' CustLocation.id IN (' . $subQuery . ') ';
        $subQueryExpression = $dbo->expression($subQuery);
        $conditions[] = $subQueryExpression;

        $custLocations = $this->CustLocation->find('list', array('conditions' => $conditions, 'order' => 'CustLocation.name'));

        // ไม่มีข้อมูล CPSLocation สำหรับ CustProductSite นี้
        if (empty($custLocations)) {
            // ดึงรายการ CustLocation ทั้งหมดของ Site นี้แทน
            $this->CustProductSite->id = $cust_product_site_id;
            $site_id = $this->CustProductSite->field('site_id');
            $custLocations = $this->CustLocation->find('list', array('conditions' => array('site_id' => $site_id), 'order' => 'CustLocation.name'));
        }

        parent::option_list($custLocations);
    }
}
?>
